package com.example.hasee.plant.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.plant.R;
import com.example.hasee.plant.adapter.DataListAdapter;
import com.example.hasee.plant.config.MSharePreference;
import com.example.hasee.plant.model.SaveDataModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by hasee on 2017-02-03.
 */

public class DataListActivity extends Activity{
    TextView tv_back,tv_clear;
    ListView listView;
    List<SaveDataModel> list;

    MSharePreference sharePreference;
    DataListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.datalist_activity);
        tv_back= (TextView) findViewById(R.id.tv_back);
        listView= (ListView) findViewById(R.id.datalist);
        tv_clear= (TextView) findViewById(R.id.tv_clear);
        sharePreference=new MSharePreference(getApplicationContext());

        list=new ArrayList<>();
        setData();
        onListener();
    }

    private void onListener(){
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        tv_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MSharePreference.clearAll(getApplicationContext());
                if(adapter!=null){
                    clearFile();
                    list.clear();
                    adapter.notifyDataSetChanged();
                }
                Toast.makeText(DataListActivity.this,"清空数据成功",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void setData(){
        Gson gson=new Gson();
        Type type= new TypeToken<SaveDataModel>(){}.getType();
        Map<String,?> map=MSharePreference.getAllData(getApplicationContext());
        if(map==null||map.size()==0){
            Toast.makeText(DataListActivity.this,"当前没有数据",Toast.LENGTH_SHORT).show();
        }else{
            for(String key : map.keySet()){
                Log.d("datamap","key="+key+",value="+map.get(key)+"\n");
                SaveDataModel model=gson.fromJson(map.get(key)+"",type);
                list.add(model);
            }
            setAdapter();
        }
    }

    private void setAdapter(){
        adapter=new DataListAdapter(DataListActivity.this,list);
        if(adapter!=null){
            listView.setAdapter(adapter);
        }
    }

    private void clearFile(){
        for(SaveDataModel model: list){
            File imgfile=new File(model.getImgPath());
            File recordfile=new File(model.getRecordPath());
            File videofile=new File(model.getVideoPath());
            if(imgfile.exists()){
                imgfile.delete();
            }
            if(recordfile.exists()){
                recordfile.delete();
            }
            if(videofile.exists()){
                videofile.delete();
            }
        }
    }
}
